import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
import time
import uuid

# MongoDB配置 - 请替换为您的实际信息
MONGO_HOST = "47.111.13.97"
MONGO_PORT = 27017
MONGO_USER = "admin"
MONGO_PASSWORD = "123456"
MONGO_DB = "admin"
MONGO_COLLECTION = "comments"


def get_all_view_info():
    # 修正URL格式 - 使用正确的评论页URL
    base_url = 'https://movie.douban.com/subject/35660795/comments'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'Referer': 'https://movie.douban.com/subject/35660795/'
    }

    all_view_list = []  # 存储所有评价

    # 豆瓣评论分页逻辑：start=0, 20, 40...
    for start in range(0, 100, 20):
        page = start // 20 + 1
        url = f"{base_url}?start={start}&limit=20&status=P&sort=new_score"
        print(f"正在爬取第 {page} 页: {url}")

        try:
            response = requests.get(url, headers=headers, timeout=10)
            if response.status_code == 200:
                soup = BeautifulSoup(response.text, 'html.parser')
                view_list = []

                # 提取评论内容和评分
                comment_items = soup.select('div.comment-item')
                for item in comment_items:
                    try:
                        # 提取评论内容
                        content_span = item.select_one('span.short')
                        content = content_span.text.strip() if content_span else "无评价内容"

                        # 提取评分
                        rating_tag = item.select_one('span.rating')
                        rating = rating_tag.get('title') if rating_tag else "未评分"

                        # 提取用户名
                        username = item.select_one('span.comment-info a').text.strip()

                        # 提取评论时间
                        time_span = item.select_one('span.comment-time')
                        comment_time = time_span.text.strip() if time_span else ""

                        # 构建评论数据
                        view_info = {
                            "id": str(uuid.uuid4()),  # 生成唯一ID
                            "movie_id": "35660795",
                            "username": username,
                            "content": content,
                            "rating": rating,
                            "time": comment_time,
                            "type": classify_rating(rating),  # 根据评分分类
                            "is_fake": False  # 默认不是水军评论
                        }

                        view_list.append(view_info)
                    except Exception as e:
                        print(f"解析评论时出错: {e}")
                        continue

                all_view_list.extend(view_list)
                print(f"第 {page} 页爬取完成，共 {len(view_list)} 条评价")

                # 如果没有数据，提前结束
                if len(view_list) == 0:
                    break
            else:
                print(f"第 {page} 页请求失败，状态码: {response.status_code}")
                if response.status_code == 403:
                    print("可能被豆瓣反爬机制拦截，请稍后再试或更换IP")
                    break
        except Exception as e:
            print(f"爬取第 {page} 页时出错: {e}")
            continue

        # 控制爬取速度，避免被封IP
        time.sleep(2)

    return all_view_list


def classify_rating(rating_text):
    """根据评分文本分类评价类型"""
    rating_map = {
        "力荐": "好评",
        "推荐": "好评",
        "还行": "中评",
        "较差": "差评",
        "很差": "差评"
    }
    return rating_map.get(rating_text, "未评分")


def save_to_mongodb(data_list):
    """将评论数据保存到MongoDB"""
    if not data_list:
        print("没有数据需要保存")
        return

    try:
        # 连接MongoDB
        client = MongoClient(
            host=MONGO_HOST,
            port=MONGO_PORT,
            username=MONGO_USER,
            password=MONGO_PASSWORD,
            authSource="admin"  # 如果使用认证，通常是admin数据库
        )

        # 选择数据库和集合
        db = client[MONGO_DB]
        collection = db[MONGO_COLLECTION]

        # 批量插入数据
        result = collection.insert_many(data_list)
        print(f"成功保存 {len(result.inserted_ids)} 条评论到MongoDB")

        # 关闭连接
        client.close()

    except Exception as e:
        print(f"保存到MongoDB时出错: {e}")


def generate_fake_comments(movie_id, count=10):
    """生成测试用的水军评论"""
    fake_comments = []
    fake_users = ["水军一号", "水军二号", "水军三号", "水军四号", "水军五号"]

    for i in range(count):
        fake_comment = {
            "id": str(uuid.uuid4()),
            "movie_id": movie_id,
            "username": fake_users[i % len(fake_users)],
            "content": f"这是一条水军评论#{i + 1}：电影太棒了，强烈推荐！",
            "rating": "力荐",
            "time": time.strftime("%Y-%m-%d %H:%M:%S"),
            "type": "好评",
            "is_fake": True
        }
        fake_comments.append(fake_comment)

    return fake_comments


if __name__ == '__main__':
    # 爬取真实评论
    all_data = get_all_view_info()
    print(f"总共爬取 {len(all_data)} 条真实评论")

    # 生成一些测试用的水军评论
    fake_comments = generate_fake_comments("35660795", count=5)
    all_data.extend(fake_comments)
    print(f"添加了 {len(fake_comments)} 条测试用的水军评论")

    # 保存到MongoDB
    save_to_mongodb(all_data)

    # 打印前5条数据示例
    print("\n前5条评论示例:")
    for i, info in enumerate(all_data[:5], 1):
        print(f"\n评论 #{i}:")
        print(f"用户名: {info['username']}")
        print(f"评分: {info['rating']}")
        print(f"内容: {info['content']}")
        print(f"类型: {info['type']}")
        print(f"是否水军: {info['is_fake']}")